<?php

chdir('../');

//var_dump($_REQUEST);



require('includes/setUp.php');


$err = false;

try {
  
  //throw new Exception('hhh');
  $msg = null;
  
  if (!isset($_FILES['Filedata'])) $msg = 'Nothing to upload';
 
  switch ($_FILES['Filedata']['error']) {
    case 0: break;
    case 1:    
    case 2:
      $msg = T_('File Size Error');     break;
    case 3:
      $msg = T_('Incomplete file');     break;
    case 4:
      $msg = T_('No file');             break;
    case 6:
      $msg = T_('No temp folder');      break;
    case 7:
      $msg = T_('Error writing file');  break;
    case 8:
      $msg = T_('Invalid extension');   break;
    default:
      $msg = T_('unkown error');        break;
    }
  
  if($msg !== null) {
    $err = true;
    throw new Exception($msg);
  }   
  
  //throw new Exception('Simulated Error');
  
  $directory = 'directory'.mt_rand(1, 20).'/';
  $photo = new Photo();
  
  
  
  /*
  if ($user == null) throw new Exception(T_('You have to be logged on!'));
  if ($photo->Album->User != $user) throw new Exception(T_('You aren\'t owner of this album'));*/
  
  $image = new Image($_FILES['Filedata']['tmp_name']);
  
  $image->resizeImage($config['Picture']['Side']);
  $image->rotateImageByExif();
  $image->saveImage(Photo::ORIGINAL_PATH.$directory, null, Image::IMAGE_QUALITY_HIGH);

  
  $photo->Album        = $db->find('Album', $_POST['idAlbum']);
  $photo->FileUNCPath  = $image->OutputFile;
  $photo->FileWidth    = $image->ImageInfo->Width;
  $photo->FileHeight   = $image->ImageInfo->Height;
  $photo->FileSize     = $image->ImageInfo->Size;
  $photo->FileMimeType = $db->find('MimeType', $image->ImageInfo->MimeType);

  $image->resizeImage($config['Picture']['ThumbnailSide']);
  $image->saveImage(Photo::THUMBNAIL_PATH.$directory);

  $photo->ThumbnailUNC = $image->OutputFile;

  $image->dispose();

  $pos = strrpos($_FILES['Filedata']['name'], '.');
  $name = substr($_FILES['Filedata']['name'], 0, ($pos > 30) ? 30 : $pos);
  $photo->Name          = $name;

  
  $photo->Album->PhotoCount++;
  $photo->AverageRating = 0.0;
  $photo->RatingsCount  = 0;

  $i=0;
  
  $params = $db->getRepository('Parameter')->findAll();
  $ex = $image->ExifInfo->getAsArray();
  $photoParam = array();
  
  foreach($params as $param) {
    if (isset($ex[$param->Parameter])) {
      $photoParam[$i] = new PhotosParameters();
      $photoParam[$i]->Parameter = $param;
      $photoParam[$i++]->Value   = $ex[$param->Parameter];
    }  
  }
  /*
  if ($image->ExifInfo->Maker !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Maker');
    $photoParam[$i++]->Value = $image->ExifInfo->Maker;
  }

  if ($image->ExifInfo->Model !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Model');
    $photoParam[$i++]->Value = $image->ExifInfo->Model;
  }

  if ($image->ExifInfo->ExposureTime !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Exposure Time');
    $photoParam[$i++]->Value = $image->ExifInfo->ExposureTime;
  }

  if ($image->ExifInfo->FNumber !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','FNumber');
    $photoParam[$i++]->Value = $image->ExifInfo->FNumber;
  }

  if ($image->ExifInfo->FocalLength !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Focal Length');
    $photoParam[$i++]->Value = $image->ExifInfo->FocalLength;
  }

  if ($image->ExifInfo->DateTime !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Date Taken');
    $photoParam[$i++]->Value = $image->ExifInfo->DateTime;
  }

  if ($image->ExifInfo->ISO !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','ISO');
    $photoParam[$i++]->Value = $image->ExifInfo->ISO;
  }

  if ($image->ExifInfo->WhiteBalance !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','White Balance');
    $photoParam[$i++]->Value = $image->ExifInfo->WhiteBalance;
  }

  if ($image->ExifInfo->Flash !== null) {
    $photoParam[$i] = new PhotosParameters();
    $photoParam[$i]->Parameter = $db->find('Parameter','Flash');
    $photoParam[$i++]->Value = $image->ExifInfo->Flash;
  }*/
  
  foreach ($photoParam as $value) {
    $photo->addParameter($value);
  }

  $db->persist($photo);
  $db->flush();

} catch (Exception $ex) {
  
  $err = true;
  
  if (isset($_POST['uploadPhoto'])) {
    globalFunc::redirect(WebRoot.'album.php?idAlbum='.$_POST['idAlbum'].'&errMsg='.rawurlencode($ex->getMessage()));
    exit;
  } else {
    echo 'ERR|'.$ex->getMessage();
  }  
}

if (isset($_POST['uploadPhoto'])) 
  globalFunc::redirect();
else 
  if(!$err) 
    echo 'OK';//$photo->FileUNCPath;